using BEPUphysics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.SceneControl;
using PloobsEngine.Utils;
using PloobsEngine.Physics.Bepu;
using PloobsEngine;

namespace Etapa1
{
    /// <summary>
    /// Handles input and movement of a character in the game.
    /// Acts as a simple 'front end' for the bookkeeping and math of the character controller.
    /// </summary>
    public class CharacterControllerInput : IScreenUpdateable
    {
        /// <summary>
        /// Physics representation of the character.
        /// </summary>
        public CharacterController CharacterController;

        /// <summary>
        /// Whether or not to use the character controller's input.
        /// </summary>
        public bool IsActive = true;

        Keys frente = Keys.T;
        Keys tras = Keys.G;
        Keys direita = Keys.F;
        Keys esquerda = Keys.H;
        Keys pulo = Keys.R;


        /// <summary>
        /// Constructs the character and internal physics character controller.
        /// </summary>
        /// <param name="owningSpace">Space to add the character to.</param>
        /// <param name="cameraToUse">Camera to attach to the character.</param>
        public CharacterControllerInput(SceneScreen scene, Vector3 position, float characterHeight, float characterWidth, float mass) : base(scene)
        {
            CharacterController = new CharacterController(position,characterHeight,characterWidth,mass,1.04f, .01f, .04f);
            this.Start();
            //Deactivate();
        }

        public CharacterControllerInput(SceneScreen scene, Vector3 position, float characterHeight, float characterWidth, float mass,Vector3 scale)
            : base(scene)
        {
            CharacterController = new CharacterController(position, characterHeight, characterWidth, mass, 1.04f, .01f, .04f, scale);
            this.Start();
            //Deactivate();
        }


        /// <summary>
        /// Gives the character control over the camera and movement input.
        /// </summary>
        public void Activate()
        {
            if (!IsActive)
            {
                IsActive = true;                
                CharacterController.Activate();                
            }
        }

        /// <summary>
        /// Returns input control to the camera.
        /// </summary>
        public void Deactivate()
        {
            if (IsActive)
            {
                IsActive = false;                
                CharacterController.Deactivate();
            }
        }


        public override void Update(GameTime gameTime)
        {
            Update(gameTime.ElapsedGameTime.Milliseconds);
        }

        public void Update(float dt )
        {
            if (IsActive)
            {
                KeyboardState keyboardInput = Keyboard.GetState();                
                       
                Vector2 totalMovement = Vector2.Zero;                
                Vector2 mv = VectorUtils.ToVector2(CharacterController.Body.OrientationMatrix.Forward);
                Vector2 lado = VectorUtils.Perpendicular2DNormalized(mv);

                if (keyboardInput.IsKeyDown(frente))
                {                    
                    totalMovement -= mv;
                }
                if (keyboardInput.IsKeyDown(tras))
                {
                    totalMovement += mv;
                }
                if (keyboardInput.IsKeyDown(esquerda))
                {                 
                    //totalMovement += lado;
                    CharacterController.RotateYByAngleDegrees(-1);
                }
                if (keyboardInput.IsKeyDown(direita))
                {
                    //totalMovement -= lado;
                    CharacterController.RotateYByAngleDegrees(1);
                }
                if (totalMovement == Vector2.Zero)
                    CharacterController.MovementDirection = Vector2.Zero;
                else
                    CharacterController.MovementDirection = Vector2.Normalize(totalMovement);

                

                //Jumping
                if (keyboardInput.IsKeyDown(pulo))
                {
                    CharacterController.Jump();
                }


            }
        }
    }
}